package com.ukefu.util.ai;

import java.io.IOException;
import java.util.List;

import org.lionsoul.jcseg.tokenizer.core.JcsegException;

import com.ukefu.core.UKDataContext;
import com.ukefu.webim.service.cache.CacheHelper;
import com.ukefu.webim.service.repository.AiConfigRepository;
import com.ukefu.webim.service.repository.SceneItemRepository;
import com.ukefu.webim.web.model.AiConfig;
import com.ukefu.webim.web.model.SceneItem;

public class AiUtils {
	private static AiDicTrie aiDicTrie = new AiDicTrie();
	
	/**
	 * 初始化 AI语料库
	 * @param orgi
	 * @throws IOException
	 * @throws JcsegException 
	 */
	public static AiDicTrie init(String orgi) throws IOException, JcsegException{
		aiDicTrie.clean();
		SceneItemRepository sceneItemRes = UKDataContext.getContext().getBean(SceneItemRepository.class) ;
		List<SceneItem> sceneItemList = sceneItemRes.findByOrgiAndItemtype(orgi, UKDataContext.AiItemType.USERINPUT.toString()) ;
		for(SceneItem item : sceneItemList){
			aiDicTrie.insert(item.getContent(), item.getSceneid());
		}
		return aiDicTrie;
	}
	
	public static AiDicTrie getAiDicTrie(){
		return aiDicTrie ;
	}
	
	
	/**
	 * AI配置
	 * @param orgi
	 * @return
	 */
	public static AiConfig initAiConfig(String orgi){
		AiConfig aiConfig = null;
		if(UKDataContext.getContext() != null && (aiConfig = (AiConfig) CacheHelper.getSystemCacheBean().getCacheObject(UKDataContext.SYSTEM_CACHE_AI_CONFIG, orgi)) == null){
			AiConfigRepository aiConfigRepository = UKDataContext.getContext().getBean(AiConfigRepository.class) ;
			aiConfig = aiConfigRepository.findByOrgi(orgi) ;
			if(aiConfig == null){
				aiConfig = new AiConfig() ;
			}else{
				CacheHelper.getSystemCacheBean().put(UKDataContext.SYSTEM_CACHE_AI_CONFIG,aiConfig, orgi) ;
			}
		}
		return aiConfig ;
	}
}
